import { IOption, IFormItem, FormItemType } from '@shared/interfaces/common'
import { numScale, compareTwoCurrencies } from 'app/utils/helpers'
import { UPIChannel } from '@shared/interfaces/acquirer'
import { Validators } from '@angular/forms'
import {
  AcquiringIINMaxLength,
  AcquiringIINReg,
  ForwardingIINMaxLength,
  ForwardingIINReg,
  ChannelMerchantNameMaxLength,
  TransactionFeeReg,
  ChannelSignPublicKeyIDMaxLength,
  ChannelSignPublicKeyIDReg,
  ChannelEncryptPublicKeyIDMaxLength,
  ChannelEncryptPublicKeyIDReg,
} from 'app/utils/contraints'

export const genUPIFormItems = (
  currencyOptions: IOption[],
  currencyCodeOptions: IOption[],
  handleChanMerIDChange,
): IFormItem[] => {
  return [
    {
      label: 'Acquiring IIN',
      errTips: {
        required: '必填项',
        existed: '该渠道已存在',
        maxLength: '最大长度不超过11',
        pattern: '格式错误：只能是数字',
      },
      formControlName: 'chanMerID',
      change: handleChanMerIDChange,
    },
    {
      label: 'Forwarding IIN',
      errTips: {
        required: '必填项',
        maxLength: '最大长度不超过12',
        pattern: '格式错误：只能是数字',
      },
      formControlName: 'chanMerIDSecondary',
    },
    {
      label: 'Transaction Fee',
      errTips: {
        required: '必填项',
        pattern: '格式不正确，最多只支持四位小数,且不可为负数',
      },
      type: FormItemType.INPUTGROUP,
      addAfter: '%',
      errTip: 'Transaction Fee 只能在0-10%之间',
      formControlName: 'fee',
      inputType: 'number',
    },
    {
      label: 'Channel Sign Public Key ID',
      errTips: {
        required: '必填项',
        maxLength: '最长不超过128位',
        pattern: '格式错误：只支持数字字母',
      },
      formControlName: 'signKeyID',
    },
    {
      label: 'Channel Sign Public Key',
      formControlName: 'signKey',
      type: FormItemType.FILE,
    },
    {
      label: 'Channel Sign Encryption Public Key ID',
      errTips: {
        required: '必填项',
        maxLength: '最长不超过128位',
        pattern: '格式错误：只支持数字字母',
      },
      formControlName: 'encryptKeyID',
    },
    {
      label: 'Channel Encryption Public Key',
      formControlName: 'encryptKey',
      type: FormItemType.FILE,
    },
    {
      label: 'Transaction Currency',
      errTips: {
        required: '必填项',
      },
      formControlName: 'currencies',
      type: FormItemType.CURRENCY_SELECT,
      multiple: 'multiple',
      allowClear: true,
      compareFn: compareTwoCurrencies,
      options: currencyOptions,
    },
    {
      label: 'Settlement Currency',
      errTips: {
        required: '必填项',
      },
      formControlName: 'settleCurrency',
      type: FormItemType.CURRENCY_SELECT,
      currencyType: 'string',
      multiple: 'default',
      allowClear: true,
      compareFn: compareTwoCurrencies,
    },
    /*
    {
      label: 'ownerInsCode',
      errTips: {
        required: '必填项',
      },
      errTip: 'chanInsCode 输入不正确',
      formControlName: 'chanInsCode',
    },
    */
    {
      label: 'Channel Merchant Name',
      errTips: {
        required: '必填',
        maxLength: '最大长度不超过50',
      },
      formControlName: 'chanMerName',
    },
  ]
}

export const genUPIValidateRules = (channel: UPIChannel): { [key: string]: any } => {
  return {
    id: [null, []],
    chanCode: [channel && channel.chanCode, [Validators.required]],
    chanMerID: [
      channel && channel.chanMerID,
      [Validators.required, Validators.maxLength(AcquiringIINMaxLength), Validators.pattern(AcquiringIINReg)],
    ],
    chanMerIDSecondary: [
      channel && channel.chanMerIDSecondary,
      [Validators.required, Validators.maxLength(ForwardingIINMaxLength), Validators.pattern(ForwardingIINReg)],
    ],
    chanMerName: [
      channel && channel.chanMerName,
      [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
    ],
    fee: [
      channel && numScale(channel.fee, 2),
      [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
    ],
    currencies: [channel && channel.currencies, [Validators.required]],
    settleCurrency: [channel && channel.settleCurrency, [Validators.required]],
    // chanInsCode: [channel && channel.chanInsCode, [Validators.required]],
    signKey: [channel && channel.signKey, [Validators.required]],
    encryptKey: [channel && channel.encryptKey, [Validators.required]],
    signKeyID: [
      channel && channel.signKeyID,
      [
        Validators.required,
        Validators.maxLength(ChannelSignPublicKeyIDMaxLength),
        Validators.pattern(ChannelSignPublicKeyIDReg),
      ],
    ],
    encryptKeyID: [
      channel && channel.encryptKeyID,
      [
        Validators.required,
        Validators.maxLength(ChannelEncryptPublicKeyIDMaxLength),
        Validators.pattern(ChannelEncryptPublicKeyIDReg),
      ],
    ],
    isLoaded: [ channel && channel.isLoaded],
  }
}
